package com.dq.statistics.controller;

import com.dq.commonutils.vo.Result;
import com.dq.statistics.service.StatisticsDailyService;
import com.dq.statistics.vo.ChartsParams;
import com.dq.statistics.vo.ChartsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * StatisticsDaily-API
 * @author Dq
 * @since 2022-09-15 16:38:27
 */
@RestController
@RequestMapping("staservice/sta")
// @CrossOrigin
public class StatisticsDailyController {
    @Autowired
    private StatisticsDailyService staService;

    /**
     * 调用Ucenter中查询到了某天总注册人数 然后插入当前用于图形化显示的数据库
     * @param day
     * @return
     */
    @PostMapping("registerCount/{day}")
    public Result registerCount(@PathVariable("day") String day){
        staService.registerCount(day);
        return Result.success(null);
    }

    /**
     * 用于显示最后图形的data
     * @param chartsParams
     * @return
     */
    @PostMapping("/getTotalShowDate")
    public Result getChartsDate(@RequestBody ChartsParams chartsParams){
        List<ChartsVo> chartsVo =staService.getChartsDate(chartsParams);
        List<String> arrayList = new ArrayList<>();
        List<Integer> arrayList_registerNum = new ArrayList<>();
        List<Integer> arrayList_loginNum = new ArrayList<>();
        List<Integer> arrayList_videoViewNum = new ArrayList<>();
        List<Integer> arrayList_courseNum = new ArrayList<>();
        for (ChartsVo vo : chartsVo) {
            arrayList.add(vo.getDateCalculated());
            arrayList_registerNum.add(vo.getRegisterNum());
            arrayList_loginNum.add(vo.getLoginNum());
            arrayList_videoViewNum.add(vo.getVideoViewNum());
            arrayList_courseNum.add(vo.getCourseNum());
        }
        HashMap<String, Object> map = new HashMap<>();
        if (!(arrayList_loginNum.get(0)==null)){
            map.put("loginNum",arrayList_loginNum);
        }
        if (!(arrayList_registerNum.get(0)==null)){
            map.put("registerNum",arrayList_registerNum);
        }
        if (!(arrayList_videoViewNum.get(0)==null)){
            map.put("videoViewNum",arrayList_videoViewNum);
        }
        if (!(arrayList_courseNum.get(0)==null)){
            map.put("courseNum",arrayList_courseNum);
        }

        map.put("xData",arrayList);
        return Result.success(map);
    }
}